#include "qsingleapplication.h"
#include "settingsdialog.h"
#include "watermarklistener.h"

#include "macro.h"
#include "global.h"

#include <QFile>
#include <QMenu>
#include <QAction>
#include <QTranslator>
#include <QSystemTrayIcon>

#ifdef Q_OS_WIN32
    #include <windows.h>
#endif
#ifdef Q_OS_LINUX
    #include "qsignalhandler.h"
#endif

int main(int argc, char *argv[])
{
    QSingleApplication app(argc, argv, WATER_PACKAGE_NAME);
    app.setApplicationName(WATER_APPLICATION_NAME);
    if (app.isRunning())
    {
        app.sendMessage("1");
        return 0;
    }

#ifdef Q_OS_WIN32
    // 设置程序关机时最后退出
    SetProcessShutdownParameters(0, 0);
#endif
#ifdef Q_OS_LINUX
    setup_unix_signal_handlers();
#endif

    // 设置全局字体
    QFont font("微软雅黑", 10);
    app.setFont(font);

    // 设置中文
    QTranslator translator;
    if (translator.load(":/translations/qt_zh_CN.qm"))
    {
        app.installTranslator(&translator);
    }

    // 设置icon
    app.setWindowIcon(QIcon(":/images/logo_0064.ico"));

//    // 设置默认css样式
//    QFile file(":/css/default.css");
//    file.open(QFile::ReadOnly);
//    app.setStyleSheet(QLatin1String(file.readAll()));

    // 初始化
    Global::instance().initTextConfig();
    Global::instance().initImageConfig();
    Global::instance().initQrcoreConfig();
    Global::instance().initDarkConfig();
    Global::instance().initMatrixConfig();

    // 水印编辑器
    SettingsDialog *settingsDialog = new SettingsDialog();
    settingsDialog->show();
    settingsDialog->initData();
    QObject::connect(&app, &QSingleApplication::messageAvailable, settingsDialog, [ = ](const QString & event)
    {
        if (event == "1")
        {
            settingsDialog->showWindow();
        }
    });

    // 水印监听
    WatermarkListener *listener = new WatermarkListener();
    listener->start();
    QObject::connect(settingsDialog, &SettingsDialog::updateWatermarkWindow, listener, &WatermarkListener::slotUpdateWatermarkWindow);

    // 创建托盘图标
    QSystemTrayIcon *tray = new QSystemTrayIcon();
    tray->setIcon(app.windowIcon());
    tray->setToolTip("桌面水印编辑器");
    tray->show();
    QObject::connect(tray, &QSystemTrayIcon::activated, settingsDialog, [ = ](QSystemTrayIcon::ActivationReason reason)
    {
        switch (reason)
        {
            case QSystemTrayIcon::Trigger:
                // 激活窗口
                settingsDialog->showWindow();
                break;
            default:
                break;
        }
    });

    // 托盘右键菜单
    QAction *openAction = new QAction("打开", nullptr);
    QAction *quitAction = new QAction("退出", nullptr);
    QObject::connect(openAction, &QAction::triggered, settingsDialog, [ = ]
    {
        // 激活窗口
        settingsDialog->showWindow();
    });
    QObject::connect(quitAction, &QAction::triggered, tray, [ = ]
    {
        // 需要先释放托盘对象，否则托盘中会有图标残留
        tray->deleteLater();
        QApplication::quit();
    });
    QMenu *trayMenu = new QMenu();
    trayMenu->addAction(openAction);
    trayMenu->addAction(quitAction);
    tray->setContextMenu(trayMenu);
    tray->showMessage("桌面水印编辑器", "桌面水印编辑器已启动，如需关闭，请在托盘中退出。",
                      QSystemTrayIcon::MessageIcon::Information, 5000);

    return app.exec();
}
